package com.prolificinteractive.materialcalendarview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import java.util.Date;

/**
 * Display one day of a {@linkplain MaterialCalendarView}
 */
public class DayView extends FrameLayout {

    private DayCheckedTextView checkedView;
    private View underLine;
    private boolean hasBeenChecked = false;

    private int color = Color.GRAY;

    public DayView(Context context) {
        this(context, null);
    }

    public DayView(Context context, AttributeSet attrs) {
        super(context, attrs);

        checkedView = new DayCheckedTextView(context);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        params.gravity = Gravity.CENTER;
        checkedView.setLayoutParams(params);

        addView(checkedView);
    }

    public void initEvent(OnCreateDateViewListener onCreateDateViewListener) {
        if (hasBeenChecked) return;
        hasBeenChecked = true;
        if (onCreateDateViewListener != null) {
            onCreateDateViewListener.onCreateViewDate(checkedView.getDate().getDate(), new CallBack() {
                @Override
                public void drawUnderLine() {
                    underLine = new View(getContext());
                    underLine.setBackgroundColor(color);
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(dpToPx(13f), dpToPx(1.5f));
                    params.gravity = Gravity.CENTER;
                    params.topMargin = dpToPx(13f);
                    underLine.setLayoutParams(params);
                    addView(underLine);
                }
            });
        }
    }

    public interface OnCreateDateViewListener {
        void onCreateViewDate(Date date, CallBack callBack);
    }

    public interface CallBack {
        void drawUnderLine();
    }

    public void setDateTextAppearance(int taId) {
        checkedView.setTextAppearance(getContext(), taId);

        TypedArray appearance = getContext().obtainStyledAttributes(taId, new int[]{android.R.attr.textColor});
        color = appearance.getColor(0, Color.GRAY);
        appearance.recycle(); // TODO
    }

    private int dpToPx(float dp) {
        return (int) (dp * getContext().getResources().getDisplayMetrics().density);
    }

    public DayCheckedTextView getCheckedView() {
        return checkedView;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec) / MonthView.COLUMN_COUNT;
        setMeasuredDimension(width, width);
        measureChildren(width, width);

        int padding = dpToPx(10f);
        checkedView.getLayoutParams().width = width - padding;
        checkedView.getLayoutParams().height = width - padding;
        checkedView.requestLayout();
    }
}
